﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;

public class SimpleSpineCard : CardBase
{

    Spine.ExposedList<Spine.Animation> anims;
    SkeletonAnimation spineAnimation;
    int animIndex = 0;

    protected override void Start()
    {
        if (!string.IsNullOrEmpty(imagePath))
        {
            StartCoroutine(Res.LoadAssetBundle(imagePath, (assetbundle) =>
            {
                string[] assetlist = assetbundle.GetAllAssetNames();
                if (assetlist.Length > 0)
                {
                    UnityEngine.Object cardObj = assetbundle.LoadAsset("SpineObj");
                    GameObject newObj = Instantiate<GameObject>(cardObj as GameObject, transform);
                    newObj.SetActive(true);
                    spineAnimation = newObj.GetComponentInChildren<SkeletonAnimation>();
                    anims = spineAnimation.skeletonDataAsset.GetSkeletonData(true).Animations;
                    assetbundle.Unload(false);
                }
            }));
        }
    }

    private void Update() {
        if(Input.GetKeyDown(KeyCode.Alpha0))
        {
            string animName = anims.Items[animIndex].Name;
            spineAnimation.AnimationName = animName;

            animIndex++;
            if(animIndex >= anims.Items.Length)
                animIndex = 0;
        }

        if(Input.GetKeyDown(KeyCode.X))
        {
            transform.localScale = new Vector3(transform.localScale.x * -1,transform.localScale.y,transform.localScale.z);
        }
    }

    protected override void ReleaseResouces()
    {
        
    }
}
